package app.auto.runner.base.utility;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.widget.Toast;


import app.auto.AndroidInstance;
import app.auto.annotation.Show;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.intf.json.JsonToMapUtils;

/**
 * Created by Administrator on 2017/11/11.
 */
public class ToastUtil extends AndroidInstance {

    private boolean canceled;
    private boolean autocancel = false;

    public ToastUtil() {
    }

    int length = Toast.LENGTH_SHORT;

    public ToastUtil setLength(int length) {
        this.length = length;
        return this;
    }


    public static ToastUtil live(Context aty) {
        return new ToastUtil(aty);
    }

    public static boolean toastenable = true;
    Context act;

    public ToastUtil(Context act) {
        this.act = act;
    }

    @Show
    public void toast(Object toast) {
        toast(toast, "");
    }

    public void up(Object toast) {
        toast(toast, "");
        position = pos.up;

    }

    public void center(Object toast) {
        toast(toast, "");
        position = pos.center;

    }

    pos position;

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        int period = Integer.parseInt(p[0].toString());
        setDuration(period);
        act = (Activity) getContext();
        setToast(toast).toast(mp);
        return null;
    }

    public ToastUtil setAutoCancel(boolean b) {
        this.autocancel = b;
        return this;
    }

    enum pos {up, center}

    Toast toast;

    public ToastUtil setToast(Toast toast) {
        this.toast = toast;
        return this;
    }

    boolean tmptoast;

    public void toast(final Object toast, String tag) {
        if (!toastenable) return;
        String show = "";
        if (toast instanceof String) {
            show = toast.toString();
        } else {
            show = JsonToMapUtils.entityToJson(toast);
        }
        tmptoast = false;
        if (this.toast == null) {
            if (!((Activity) act).isDestroyed() && !((Activity) act).isFinishing()) {


                this.toast =
                        Toast.makeText(act, show, length);
                tmptoast = true;
            } else {
                return;
            }
        }
        this.toast.setDuration(length);
        final Toast finaltoast = this.toast;

        final String finalShow = show;
        HandlerUtil.sendEmptymessage(act, new FunCallback() {
            @Override
            public void simpleRun(Object aty, Object[] p) {
                super.simpleRun(aty);
                if (!tmptoast) {
                    finaltoast.cancel();
                }
                if (position == pos.up) {
                    finaltoast.setGravity(Gravity.TOP, 0, 200);
                } else if (position == pos.center) {
                    finaltoast.setGravity(Gravity.CENTER, 0, 200);
                }
                finaltoast.show();
                if (autocancel) {
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            finaltoast.cancel();
                        }
                    }, duration - 200);
                }

            }
        });
//        Log.d(act.getClass().getSimpleName(), show);
        if (tag.equals("umeng")) {
        }
    }


    long duration;

    public ToastUtil setDuration(long duration) {
        this.duration = duration;
        return this;
    }
}
